<?php

  //this file defines the recordset class that we use to hold important
  //info about a recordset to facilitate displaying it...
  include_once('./libraries/displayUtils.inc.php');

  class recordset {
    //class vars - note private vars not allowed til php 5
    var $fieldNames;
    var $data;
    var $recordCount;
    //we need some way to specify the drill-down links     
    var $linkFiles;
    var $linkParams;
    var $totalCount;

    function getData() {
		return $this->data;
	}

    function setLinkParams($linkFieldIndex, $linkAttrs, $linkSources, $linkFile) {
      if (!isset($this->linkParams)) {
        $this->linkParams = array();
      }
      if (!isset($this->linkFiles)) {
        $this->linkFiles = array();
      }
      $this->linkFiles[$linkFieldIndex] = $linkFile;

      for ($i=0; $i<$this->recordCount; $i++) {
        if (!isset($this->linkParams[$i])) {
          $this->linkParams[$i] = array();
        }
        $linkString = "";
        for ($j = 0; $j < count($linkAttrs); $j++) {
          if ($j != 0) $linkString = $linkString . "&";
          $linkString = $linkString . $linkAttrs[$j] . "=" . rawurlencode($this->data[$i][$linkSources[$j]]);
        }
        $this->linkParams[$i][$linkFieldIndex] = $linkString;
      }
    }
    /*
    function simpleSetLinkParams($linkFieldIndex, $linkAttr) {
      if (!isset($this->linkParams)) {
	$this->linkParams = array();
      }
      for ($i=0; $i < $this->recordCount; $i++) {
	if (!isset($this->linkParams[$i])) {
 	  $this->linkParams[$i] = array();
        }
        $this->linkParams[$i][$linkFieldIndex] = "" . $linkAttr . "=" . $this->data[$i][$linkFieldIndex];
	//echo "{$this->linkParams[$i][$linkFieldIndex]}";
      }
    }
    */
    function __construct($myCount,
                         $myFields,
                         $myData) {

      //echo "in the constructor";
      $this->totalCount = $myCount;


      //unset($this->fieldNames);
      $this->fieldNames = $myFields;

      //unset($this->data);
      $this->data = $myData;

      //unset($this->recordCount);
      $this->recordCount = count($this->data);
      //echo "Size in the object is: " . $this->recordCount;
      //var_dump($this->fieldNames);
      
    }

    function printNavigablePage($title, $message, $curPage, $refreshLink) {
      setUpDisplay($title);    
      //show our message
      echo "<font size+=3>\n";
      echo "<b><center>{$message}</center></b>\n";
      //and the data query time
      echo "</font>\n";
      //and our link back to the main page
      printLink("./index.php","Take me back to the main screen!");
      if ($_SESSION["administrator"] === true) {
        echo "<br /><p>\n";
        printLink("./adminIndex.php", "Take me back to the Admin screen!");
      }
      printNewLine();

      //first figure out how many pages we have
      $pageCount = (int)(ceil($this->totalCount/DISPLAY_ROWS));
      //make sure the current page is valid...
      if ($curPage > $pageCount) $curPage = $pageCount;
      if ($curPage < 1) $curPage = 1;

      if ($this->totalCount > 0) {
        //echo "Yepper";
        $this->printNavLinks($curPage, $refreshLink);
      }

      //now print the record position...
      $startRecord = max(($curPage-1)*DISPLAY_ROWS + 1, 0);
      $endRecord = min($startRecord+DISPLAY_ROWS-1,$this->totalCount);
      if ($startRecord > $endRecord) $startRecord = $endRecord;
      //print out the message for the recordset  
      echo "<p> Records " . ($startRecord) . " to " . ($endRecord) . " of " . $this->totalCount . ". <p>\n";

  
      $this->printTable();
    }

    function printRecordAsVertical($recordToPrint) {
      echo "\n<!-- START printRecordAsVertical CODE -->\n";
      echo "<br /><table border='1'>\n";
      echo "<tr>";
      //here are the column headers
      echo "<th>Attribute</th><th>Value</th></tr>\n";
      //now the data
      if($recordToPrint>count($this->data) || $recordToPrint<1) {
        echo "<tr><td colspan=2><i>No data to display.</i></td></tr>\n";
      } else {
        //now do the records
        for($i=0; $i<count($this->fieldNames);$i++) {
          echo "<tr><td>{$this->fieldNames[$i]}</td>";
          $curValue = $this->data[$recordToPrint-1][$i];
          $curValue = trim($curValue);
          if (isset($curValue) && ctype_print($curValue)) {
     	    $curValue=wordwrap($curValue, 20, "\n", 1);
            echo "<td>{$curValue}</td></tr>\n";
            //echo "<td>{$this->data[$recordToPrint-1][$i]}</td></tr>\n";
          } else {
	    echo "<td><i>NULL</i></td></tr>\n";
          }
        }
      }
      echo "</table>\n";
      echo "<!-- END printRecordAsVertical CODE -->\n";
    }

    function printTable() {
      echo "\n<!-- START printTable CODE -->\n";
      echo "<br /><table border='1'>\n";
      echo "<tr>";
      //here go the column headers
      foreach($this->fieldNames as $field) {
        echo "<th>{$field}</th>";
      }
      echo "</tr>\n";

      if (count($this->data)==0) {
	echo "<tr><td colspan=" . count($this->fieldNames) . "><i>No data to display.</i></td></tr>";
      } else {
	//now do the records
	for ($i=0; $i<$this->recordCount; $i++) {
	  echo "<tr>";
	  $record=$this->data[$i];
	  for ($j=0; $j<count($record); $j++) {
   	    $field=$record[$j];
	  //foreach($record as $field) {
	    echo "<td>";
      $field = trim($field);
	    if (isset($field) && ctype_print($field)) {
	      $field=wordwrap($field, 20, "\n", 1);
		//echo "{$this->$linkFile}";
              if (isset($this->linkFiles[$j]) && isset($this->linkParams[$i][$j])) {
		$field = "<a href=" . $this->linkFiles[$j] . "?" . $this->linkParams[$i][$j] .
		           ">" . $field . "</a>";
		//echo "second"; 
	      }
		//echo "third";
	      echo $field;
	    } else {
	      echo "<i>NULL</i>";
	    }
	    echo "</td>";
	  }
	  echo "</tr>\n";
	}        
      }
      //and finally end the table
      echo "</table>\n";
      echo "<!-- END printTable CODE -->\n";
    }

    function printNavLinks($curPage, $refreshLink) {
      //print nav links code here
      //first figure out how many pages we have
      $pageCount = (int)(ceil($this->totalCount/DISPLAY_ROWS));
      //make sure the current page is valid...
      if ($curPage > $pageCount) $curPage = $pageCount;
      //echo "{$curPage}";
      //echo "$pageCount";    
      //now figure out which page links to start and end with
      //make sure first is at least 1
      $firstPage=max($curPage-((int)(DISPLAY_PAGES/2)), 1);

        //make sure last is at most the last page
      $lastPage=min($firstPage+DISPLAY_PAGES-1,$pageCount);

        //now make sure that first is <= last
      if ($firstPage > $lastPage) {
        $firstPage=max($lastPage-DISPLAY_PAGES+1,1);
      }
      //echo "First page: $firstPage Last page: $lastPage";
      //now print out the actual links
      echo "<br /><br>Display Page: \n";
      //if we do not have a link to the first page, print the first link
      if ($firstPage != 1) {
        printLink($refreshLink, "First");
        printNewLine();
      }
      //if we are not on the first page, print out the prev link
      if ($curPage != 1) {
        echo "&nbsp";
        printLink($refreshLink . "?dP=" . ($curPage-1), "Prev(" . ($curPage-1) . ")");
        printNewLine();
      }

      //now print out the page links
      for ($curLink=$firstPage; $curLink<=$lastPage; $curLink++) {
        echo "&nbsp";
        if ($curLink != $curPage) {
          printLink($refreshLink . "?dP=" . $curLink, $curLink);
        } else {
          echo  " {$curLink}";
        }
        printNewLine();
      }

      //now if we are not currently on the last page, show the next link
      if ($curPage != $lastPage) {
        echo "&nbsp";
        printLink($refreshLink . "?dP=" . ($curPage+1), "Next(" . ($curPage+1) . ")");
        printNewLine();
      }
      //now if we are don't have a link to the last page, print the Last link
      if ($lastPage != $pageCount) {
        echo "&nbsp";
        printLink($refreshLink . "?dP=" . $pageCount, "Last(" . $pageCount . ")");
        printNewLine();
      }

      echo "</br>\n";
    }


    function __destruct() {
     //nothing to do right now
    }
  } 
